package sk.tuke.constraintbuilder.analyzer;

import java.io.File;
import java.util.HashSet;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.ElementChangedEvent;
import org.eclipse.jdt.core.IElementChangedListener;
import org.eclipse.jdt.core.IJavaElementDelta;
import org.eclipse.jdt.core.JavaModelException;


public class JavaModelChangeListener implements IElementChangedListener {
	@Override
	public void elementChanged(ElementChangedEvent event) {	
		IJavaElementDelta delta = event.getDelta();
		IResource resource = delta.getElement().getResource();
		if ((delta.getFlags() & IJavaElementDelta.F_CHILDREN) != 0) {
			try {
				IProject project = resource.getProject();
				if(project!=null){
					File schema = RulesetManager.getInstance().getSchema(project.getName());
					if(schema!=null){
						new Analyzer(schema).examineResource(resource);
						HashSet<IResource> subClasses = RulesetManager.getInstance().getSubClassesForResource(resource);
						if(subClasses!=null){
							for (IResource subClass : subClasses) {
								new Analyzer(schema).examineResource(subClass);
							}
						}
					}
				}
			} catch (JavaModelException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
